package com.edusite
import com.megatome.grails.RecaptchaService
import com.edusite.Cursos
import com.edusite.Asignaturas

class AlumnosController {
          RecaptchaService recaptchaService
          def recaptchaOK = false



    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

   
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        if (session.user.tipo != "admin")
    	redirect(uri:"/")
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [alumnosInstanceList: Alumnos.list(params), alumnosInstanceTotal: Alumnos.count()]
    }



    def create = {
        if (!session.user)
        redirect(uri:"/")

        else if (session.user.tipo != "admin")
    	redirect(uri:"/")

        def alumnosInstance = new Alumnos()
        alumnosInstance.properties = params
        return [alumnosInstance: alumnosInstance]
    }
    
    def createPublic = {
 
        def alumnosInstance = new Alumnos()
        alumnosInstance.properties = params
        return [alumnosInstance: alumnosInstance]
    }

    def save = { 
       if (session.user.tipo != "admin")  
              redirect(uri:"/") 
 
        def alumnosInstance = new Alumnos(params)  
        if (alumnosInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), alumnosInstance.id])}"
             redirect(action: "show", id: alumnosInstance.id)
        }
        else {
            render(view: "create", model: [alumnosInstance: alumnosInstance])
        }
    }
    
   def savePublic = {

          def alumnosInstance = new Alumnos(params)
	  recaptchaOK=true
        if (!recaptchaService.verifyAnswer(session,request.getRemoteAddr(), params)) {
             recaptchaOK=false
             }     
            
        if (recaptchaOK) {
             
            if (alumnosInstance.save(flush: true)) {
              def solicitud ="nueva alta en el entorno"
              sendMail {
                 to npersona.correoe , "ent.educativo@gmail.com"
                 subject "Entorno Educativo, envio de información"
                 body (view:"/personas/enviopassword",
                 model:[npersona:alumnosInstance, solicitud:solicitud])
              }
              sendMail {
                 to "ent.educativo@gmail.com"
                 subject "Entorno Educativo, envio de información"
                 body (view:"/personas/enviopassword",
                 model:[npersona:alumnosInstance, solicitud:solicitud])
              }
           
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), alumnosInstance.id])}"
            recaptchaOK=false
            redirect(action: "showPublic", id: alumnosInstance.id)
            }
         }
        
        else {
            render(view: "create", model: [alumnosInstance: alumnosInstance])
        }
    }


    def show = {
         if (session.user.tipo != "admin" && session.user.id != params.id)
    	    redirect(uri:"/")       
        def alumnosInstance = Alumnos.get(params.id)
        if (!alumnosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [alumnosInstance: alumnosInstance]
        }
    }

    def showPublic = {
        if (!recaptchaOK)
    	    redirect(uri:"/")    
    	       
        def alumnosInstance = Alumnos.get(params.id)
        if (!alumnosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
            redirect(action: "list")
        }
        else {
            [alumnosInstance: alumnosInstance]
        }
    }

    def edit = {
        
        def alumnosInstance = Alumnos.get(params.id)
        if (!alumnosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [alumnosInstance: alumnosInstance]
        }
    }

    def update = {
        def alumnosInstance = Alumnos.get(params.id)
        if (alumnosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (alumnosInstance.version > version) {
                    
                    alumnosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'alumnos.label', default: 'Alumnos')] as Object[], "Another user has updated this Alumnos while you were editing")
                    render(view: "edit", model: [alumnosInstance: alumnosInstance])
                    return
                }
            }
            alumnosInstance.properties = params
            if (!alumnosInstance.hasErrors() && alumnosInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), alumnosInstance.id])}"
                redirect(action: "show", id: alumnosInstance.id)
            }
            else {
                render(view: "edit", model: [alumnosInstance: alumnosInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        if (session.user.tipo != "admin")
    	redirect(uri:"/")
        def alumnosInstance = Alumnos.get(params.id)
        if (alumnosInstance) {
            try {
                alumnosInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
            redirect(action: "list")
        }
    }
 
    
    def indice ={
	if (!session.user){
	   redirect(url: "/educativo")
	   return
	   }
	else if (session.user.tipo =="profesor"){
	   redirect(controller:"profesores", action:"indice")
	   return
	   }
	   
 	def alumnosInstance = Alumnos.findByUsuario(session.user.usuario)
        if (!alumnosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
            redirect(url: "/")
        }
        else {
            params.max = Math.min(params.max ? params.int('max') : 10, 100) 
            params.sort="fecharealiz"
            params.order="asc"
            def listado=Avisos.findAllByEnviadoporOrAmbito(session.user,'privado', params)
            listado=listado+Avisos.findAllByAmbito('publico',params)
            def curriculumsInstance=Curriculums.findByPersona(alumnosInstance)
           
            
            [alumnosInstance:alumnosInstance, avisosInstanceList:listado , avisosInstanceTotal: Avisos.count(), curriculumsInstance:curriculumsInstance]   

             
         }    

    }
    def matricula = {

         if ((session.user.tipo != "admin") && (session.user.id.toString() != params.id)){ 
   	    redirect(uri:"/")   
   	 }  

        def alumnosInstance = Alumnos.get(params.id)
        if (!alumnosInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'alumnos.label', default: 'Alumnos'), params.id])}"
            redirect(action: "list")
            return
        }
	def cursosInstanceList
	def asigInstanceList
        if(!params.idCurso)
	cursosInstanceList = Cursos.list(params)
	else {
	cursosInstanceList = Cursos.get(params.idCurso)
	asigInstanceList = Asignaturas.findAllByCurso(cursosInstanceList)
	}
	
	
            [alumnosInstance: alumnosInstance, cursosInstanceList: cursosInstanceList, asigInstanceList:asigInstanceList ]

    }
    

    
}
